﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace iEN.StaticReport
{
    public partial class Download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string FilePath = Request["FilePath"].ToString();
            Export(FilePath);
        }

        private void Export(string FilePath)
        {
            string FileName = Path.GetFileName(FilePath);
            try
            {
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                byte[] data=File.ReadAllBytes(FilePath);
                Response.BufferOutput = true;
                Response.OutputStream.Write(data, 0, data.Length);
                Response.Flush();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                File.Delete(FilePath);
            }
        } 
    }
}
